package com.pan.admin.module.crm.controller;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Empty;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.date.format.DateTimeFormat;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.github.pagehelper.PageInfo;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.entity.SysDept;
import com.pan.admin.common.core.domain.entity.SysUser;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.crm.entity.po.CrmStatisticDeptPO;
import com.pan.admin.module.crm.entity.po.CrmStatisticUserPO;
import com.pan.admin.module.crm.entity.qo.CrmStatisticDeptQO;
import com.pan.admin.module.crm.entity.qo.CrmStatisticUserQO;
import com.pan.admin.module.crm.entity.vo.CrmStatisticDeptVO;
import com.pan.admin.module.crm.entity.vo.CrmStatisticUserVO;
import com.pan.admin.module.crm.entity.vo.ReqCrmStatisticQueryVO;
import com.pan.admin.module.crm.service.CrmStatisticService;
import com.pan.admin.module.system.service.ISysDeptService;
import com.pan.admin.module.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  CRM数据统计
 *
 *  @author Pan
 */
@RestController
@RequestMapping("/crm/statistic")
public class CrmStatistic extends BaseController {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private CrmStatisticService crmStatisticService;

    /**
     *  员工进量统计列表
     *
     *  @param  reqCrmStatisticQueryVO  统计基础查询
     *  @return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('crm:statistic:user')")
    @GetMapping("/user")
    public TableDataInfo userStatistic(ReqCrmStatisticQueryVO reqCrmStatisticQueryVO) {
        startPage();

        Integer productId = SecurityUtils.getLoginUser().getUser().getProductId();
        SysUser sysUser = new SysUser();
        sysUser.setProductId(productId);
        sysUser.setDeptId(reqCrmStatisticQueryVO.getDeptId());
        sysUser.setNickName(reqCrmStatisticQueryVO.getNickName());
        sysUser.setPhonenumber(reqCrmStatisticQueryVO.getPhonenumber());

        List<SysUser> list = userService.selectUserList(sysUser);
        PageInfo<SysUser> sysPageInfo = new PageInfo<SysUser>(list);
        if (ValidParam.isEmpty(list)) {
            return getDataTable(Empty.list());
        }

        String startTime = String.valueOf(reqCrmStatisticQueryVO.getParams().get("beginTime"));
        String endTime = String.valueOf(reqCrmStatisticQueryVO.getParams().get("endTime"));
        boolean hasNow = false;
        if (ValidParam.isEmpty(startTime) && ValidParam.isEmpty(endTime)) {
            String date = DateUtils.currentTime(DateTimeFormat.DATE_FULL);
            startTime = date + " 00:00:00";
            endTime = DateUtils.currentTime();
            hasNow = true;
        } else {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 00:00:00";
        }

        String displayDateTime;
        //  拼接时间
        if (hasNow) {
            displayDateTime = "实时统计";
        } else {
            displayDateTime = startTime + "至" + endTime;
        }

        //  查询对应数据统计
        CrmStatisticUserQO crmStatisticUserQO = new CrmStatisticUserQO();
        crmStatisticUserQO.setListUserId(CollUtils.toList(list, t -> t.getUserId()));
        crmStatisticUserQO.setStartTime(startTime);
        crmStatisticUserQO.setEndTime(endTime);
        crmStatisticUserQO.setLoanProductId(productId);
        List<CrmStatisticUserPO> listCrmStatistic = crmStatisticService.userStatistic(crmStatisticUserQO);

        //  转换数据
        List<CrmStatisticUserVO> listCrmStatisticUserVO = CopyUtils.copyList(list, CrmStatisticUserVO.class);
        IteratorUtils.array(listCrmStatisticUserVO, t -> {
            CrmStatisticUserPO find = CollUtils.findOne(listCrmStatistic, crmStatisticDeptPO -> Eq.object(crmStatisticDeptPO.getUserId(), t.getUserId()));
            if (ValidParam.isNotNull(find)) {
                t.setApplyTotal(find.getApplyTotal());
                t.setUserId(null);
            }
            t.setCreateTime(displayDateTime);
        });

        return getDataTable(listCrmStatisticUserVO, sysPageInfo.getTotal());
    }

    /**
     *  部门进量统计列表
     *
     *  @param  reqCrmStatisticQueryVO  统计基础查询
     *  @return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('crm:statistic:dept')")
    @GetMapping("/dept")
    public TableDataInfo deptStatistic(ReqCrmStatisticQueryVO reqCrmStatisticQueryVO) {
        startPage();

        Integer productId = SecurityUtils.getLoginUser().getUser().getProductId();
        SysDept sysDept = new SysDept();
        sysDept.setProductId(productId);
        sysDept.setDeptId(reqCrmStatisticQueryVO.getDeptId());
        sysDept.setDeptName(reqCrmStatisticQueryVO.getDeptName());

        List<SysDept> listDept = deptService.selectDeptList(sysDept);
        PageInfo<SysDept> sysPageInfo = new PageInfo<SysDept>(listDept);
        if (ValidParam.isEmpty(listDept)) {
            return getDataTable(Empty.list());
        }

        String startTime = String.valueOf(reqCrmStatisticQueryVO.getParams().get("beginTime"));
        String endTime = String.valueOf(reqCrmStatisticQueryVO.getParams().get("endTime"));
        boolean hasNow = false;
        if (ValidParam.isEmpty(startTime) && ValidParam.isEmpty(endTime)) {
            String date = DateUtils.currentTime(DateTimeFormat.DATE_FULL);
            startTime = date + " 00:00:00";
            endTime = DateUtils.currentTime();
            hasNow = true;
        } else {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 00:00:00";
        }

        String displayDateTime;
        //  拼接时间
        if (hasNow) {
            displayDateTime = "实时统计";
        } else {
            displayDateTime = startTime + "至" + endTime;
        }

        //  查询对应数据统计
        CrmStatisticDeptQO crmStatisticDeptQO = new CrmStatisticDeptQO();
        crmStatisticDeptQO.setListDeptId(CollUtils.toList(listDept, t -> t.getDeptId()));
        crmStatisticDeptQO.setStartTime(startTime);
        crmStatisticDeptQO.setEndTime(endTime);
        crmStatisticDeptQO.setLoanProductId(productId);
        List<CrmStatisticDeptPO> listCrmStatistic = crmStatisticService.deptStatistic(crmStatisticDeptQO);

        //  转换数据
        List<CrmStatisticDeptVO> listCrmStatisticDeptVO = CopyUtils.copyList(listDept, CrmStatisticDeptVO.class);
        IteratorUtils.array(listCrmStatisticDeptVO, t -> {
            CrmStatisticDeptPO find = CollUtils.findOne(listCrmStatistic, crmStatisticDeptPO -> Eq.object(crmStatisticDeptPO.getDeptId(), t.getDeptId()));
            if (ValidParam.isNotNull(find)) {
                t.setApplyTotal(find.getApplyTotal());
                t.setEmployeeTotal(find.getEmployeeTotal());
                t.setDeptId(null);
            }
            t.setCreateTime(displayDateTime);
        });
        return getDataTable(listCrmStatisticDeptVO, sysPageInfo.getTotal());
    }
}
